<template>
  <div>
    <el-row>
      <el-col :span="7"
        ><div class="grid-content bg-purple-dark">&emsp;</div></el-col
      >
      <el-col :span="10">
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;

        <div>
          <el-row :gutter="20">
            <el-col :span="4"
              ><div class="grid-content bg-purple">
                &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; 用户头像
              </div></el-col
            >
            <el-col :span="10"
              ><div class="grid-content bg-purple">&emsp;</div></el-col
            >
            <el-col :span="6">
              <el-tooltip
                class="item"
                effect="dark"
                content="点击修改头像"
                placement="right"
              >
                <div class="demo-basic--circle" @click="dialogFlag = true">
                  <div class="block">
                    <el-avatar :size="50" :src="userShow.userImg"></el-avatar>
                  </div>
                </div>
              </el-tooltip>
            </el-col>
          </el-row>
          <el-divider></el-divider>
          <span></span>
        </div>
        <div>
          <el-row :gutter="20">
            <el-col :span="4"
              ><div class="grid-content bg-purple">用户名</div></el-col
            >
            <el-col :span="10"
              ><div class="grid-content bg-purple">&emsp;</div></el-col
            >
            <el-col :span="6"
              ><div class="grid-content bg-purple">
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="点击修改昵称"
                  placement="right"
                >
                  <span @click="changeUserName()">{{ userShow.userName }}</span>
                </el-tooltip>
              </div></el-col
            >
          </el-row>
          <el-divider></el-divider>
          <span></span>
        </div>
        <div>
          <el-row :gutter="20">
            <el-col :span="4"
              ><div class="grid-content bg-purple">手机号</div></el-col
            >
            <el-col :span="10"
              ><div class="grid-content bg-purple">&emsp;</div></el-col
            >
            <el-col :span="6"
              ><div class="grid-content bg-purple">
                {{ userShow.userTel }}
              </div></el-col
            >
          </el-row>
          <el-divider></el-divider>
          <span></span>
        </div>
        <div>
          <el-row :gutter="20">
            <el-col :span="4"
              ><div class="grid-content bg-purple">等级</div></el-col
            >
            <el-col :span="10"
              ><div class="grid-content bg-purple">&emsp;</div></el-col
            >
            <el-col :span="6"
              ><div class="grid-content bg-purple">
                {{ userShow.userTagName }}
              </div></el-col
            >
          </el-row>
          <el-divider></el-divider>
          <span></span>
        </div>
        <div>
          <el-row :gutter="20">
            <el-col :span="4"
              ><div class="grid-content bg-purple">状态</div></el-col
            >
            <el-col :span="10"
              ><div class="grid-content bg-purple">&emsp;</div></el-col
            >
            <el-col :span="6"
              ><div class="grid-content bg-purple" v-if="userShow.userState == 1">
                正常
              </div>
              <div class="grid-content bg-purple" v-if="userShow.userState == 2">
                注销
              </div>
              <div class="grid-content bg-purple" v-if="userShow.userState == 3">
                逾期
              </div></el-col
            >
          </el-row>
          <el-divider></el-divider>
          <span></span>
        </div>
      </el-col>

      <el-col :span="7"><div class="grid-content bg-purple-dark"></div></el-col>
    </el-row>

    <el-dialog title="修改图像" :visible.sync="dialogFlag" width="30%" center>
      <el-row>
        <el-col :span="7"
          ><div class="grid-content bg-purple-dark">&emsp;</div></el-col
        >
        <el-col :span="10"
          ><el-upload
            class="avatar-uploader"
            action="http://localhost:8080/uploadoss"
            name="file"
            :show-file-list="false"
            :on-success="handleSuccess"
            :before-upload="beforeUpload"
          >
            <img v-if="imageUrl" :src="userShow.userImg" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload
        ></el-col>
        <el-col :span="7"
          ><div class="grid-content bg-purple-dark"></div
        ></el-col>
      </el-row>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFlag = false">取 消</el-button>
        <el-button type="primary" @click="dialogFlag = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imageUrl: "",
      size: "",
      user: {},
      userShow:{},
      dialogFlag: false,
    };
  },
  methods: {
    changeUserName() {
      this.$prompt("请输入新昵称", "更换昵称", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        // inputPattern: /^[\w]{3~11}?/,
        // inputErrorMessage: "昵称不能为空",
      })
        .then(({ value }) => {
          this.$axios
            .put("user/updateUser", {
              tel: this.user.userTel,
              password: this.user.userPassword,
              userName: value,
            })
            .then((res) => {
              if (res.data.code == 200) {
                this.loadUser();
                this.$message({
                  type: "success",
                  message: "你的新昵称是: " + value,
                });
              } else {
                this.$message({
                  type: "warn",
                  message: "你的新昵称是: " + value,
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    loadUser() {
      this.$axios
        .post("user/usershow", {
          tel: this.user.userTel,
          password: this.user.userPassword,
        })
        .then((res) => {
          this.userShow = res.data.data.userVo;
          this.imageUrl = this.userShow.userImg;
          console.log(res.data.data.userVo);
        });
    },
    handleSuccess(res, file) {
      this.dialogFlag = false;
      if (res.code == 200) {
        this.imageUrl = res.message;
        // this.user.headImg = res.message; //将返回的文件储存路径赋值picture字段
        this.$axios
          .put("user/updateUser", {
            tel: this.user.userTel,
            password: this.user.userPassword,
            userImg:res.message,
          })
          .then((res) => {
            this.loadUser();
          });
      }
    },
    beforeUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },

  mounted() {
    this.user = JSON.parse(sessionStorage.getItem("user"));
    this.loadUser();
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>


